package com.example.demo.data.model;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.demo.R;
import com.squareup.picasso.Picasso;

import java.util.List;

public class NewsAdapter extends ArrayAdapter<News> {

    Picasso picasso = Picasso.get();

    public NewsAdapter(@NonNull Context context, int resource, @NonNull List<News> objects) {
        super(context, resource, objects);
        picasso.setLoggingEnabled(true);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        News news = getItem(position);
        @SuppressLint("ViewHolder")
        View view = LayoutInflater.from(getContext()).inflate(R.layout.news_item, parent,false);
        if(view!=null && news !=null){
            ImageView image = view.findViewById(R.id.news_image);
            TextView name = view.findViewById(R.id.news_title);
            TextView time = view.findViewById(R.id.news_create_time);
            TextView source = view.findViewById(R.id.news_source);
            name.setText(news.getTitle());
            time.setText(news.getCreateTime());
            source.setText(news.getSource());

            if(news.getImage() == null || news.getImage().isEmpty()){
                image.setVisibility(View.GONE);
            } else {
                picasso.load(news.getImage()).into(image);
            }
        }

        return view;
    }

}
